﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.UI.StaticPartLib
{
    /// <summary>
    /// Interaction logic for PartLabel.xaml
    /// </summary>
    [System.ComponentModel.DisplayName("文本标签")]
    [XPartIcon("pack://application:,,,/CPPEI.Coolzon.UI.StaticPartLib;component/Images/PartIcons/文本标签.png")]
    public partial class PartLabel : ControlPart
    {
        private bool? _XIsShowShadow;
        private string _XShadowColor;
        private double? _XShadowDirection;
        private double? _XShadowDepth;
        /// <summary>
        /// 是否显示阴影
        /// </summary>
        [Category("Other")]
        [DisplayName("是否显示阴影")]
        [Description("IsShowShadow")]
        [Editor(typeof(UI.XProperty.Editors.XBooleanEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "IsShowShadow", "是否显示阴影")]
        public bool? XIsShowShadow 
        {
            get
            {
                return _XIsShowShadow;
            }
            set
            {
                _XIsShowShadow = value;
                Refresh();
            }
        }
        /// <summary>
        /// 阴影颜色
        /// </summary>
        [Category("Other")]
        [DisplayName("阴影颜色")]
        [Description("ShadowColor")]
        [Editor(typeof(UI.XProperty.Editors.XStringEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "ShadowColor", "阴影颜色")]
        public string XShadowColor
        {
            get
            {
                return _XShadowColor;
            }
            set
            {
                _XShadowColor = value;
                Refresh();
            }
        }
        /// <summary>
        /// 阴影方向
        /// </summary>
        [Category("Other")]
        [DisplayName("阴影方向0-360")]
        [Description("ShadowDirection")]
        [Editor(typeof(UI.XProperty.Editors.XDoubleEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "ShadowDirection", "阴影方向0-360")]
        public Double? XShadowDirection
        {
            get
            {
                return _XShadowDirection;
            }
            set
            {
                _XShadowDirection = value;
                Refresh();
            }
        }
        /// <summary>
        /// 阴影深度
        /// </summary>
        [Category("Other")]
        [DisplayName("阴影深度")]
        [Description("ShadowDepth")]
        [Editor(typeof(UI.XProperty.Editors.XDoubleEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "ShadowDepth", "阴影深度")]
        public Double? XShadowDepth
        {
            get
            {
                return _XShadowDepth;
            }
            set
            {
                _XShadowDepth = value;
                Refresh();
            }
        }
        [Category("Other")]
        [DisplayName("文本")]
        [Description("Text")]
        [Editor(typeof(UI.XProperty.Editors.XStringEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "Text", "文本")]
        public string XText
        {
            get
            {
                return ctlTextBlock.Text;
            }
            set
            {
                ctlTextBlock.Text = value;
                Refresh();
            }
        }

        [Category("Appearance")]
        [DisplayName("字体")]
        [Description("Font")]
        [Editor(typeof(UI.XProperty.Editors.XFontFamilyEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Appearance, "Font", "字体")]
        public FontFamily XFont
        {
            get
            {
                return ctlTextBlock.FontFamily;
            }
            set
            {
                if (value != null)
                {
                    ctlTextBlock.FontFamily = value;
                }
                Refresh();
            }
        }

        [Category("Other")]
        [DisplayName("字体大小")]
        [Description("FontSize")]
        [Editor(typeof(UI.XProperty.Editors.XDoubleEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "FontSize", "字体大小")]
        public double XFontSize
        {
            get
            {
                return ctlTextBlock.FontSize;
            }
            set
            {
                ctlTextBlock.FontSize = value;
                Refresh();
            }
        }

        [Category("Other")]
        [DisplayName("文本对齐")]
        [Description("TextAlign")]
        [Editor(typeof(UI.XProperty.Editors.XTextAlignmentEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "TextAlign", "文本对齐")]
        public TextAlignment XTextAlign
        {
            get
            {
                return ctlTextBlock.TextAlignment;
            }
            set
            {
                ctlTextBlock.TextAlignment = value;
                Refresh();
            }
        }

        [Category("Other")]
        [DisplayName("文本垂直对齐")]
        [Description("TextVAlign")]
        [Editor(typeof(UI.XProperty.Editors.XVerticalAlignmentEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "TextVAlign", "文本垂直对齐")]
        public VerticalAlignment XTextVAlign
        {
            get
            {
                return ctlTextBlock.VerticalAlignment;
            }
            set
            {
                ctlTextBlock.VerticalAlignment = value;
                Refresh();
            }
        }

        [Category("Other")]
        [DisplayName("字体样式")]
        [Description("FontWeight")]
        [Editor(typeof(UI.XProperty.Editors.XFontWeightEditor), typeof(UI.XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "FontWeight", "字体样式")]
        public FontWeight XFontWeight
        {
            get
            {
                return ctlTextBlock.FontWeight;
            }
            set
            {
                ctlTextBlock.FontWeight = value;
                Refresh();
            }
        }

        public PartLabel()
        {
            InitializeComponent();

            //XFontName = "微软雅黑";
            XFontSize = 12;
            XForeground = new BrushEx(new SolidColorBrush(Colors.Black));
            XText = "Label";
            //ctlTextBlock.FontFamily = new FontFamily(XFontName);
        }
        protected override void OnApplyXProperty()
        {
            if(ctlTextBlock == null)
            {
                return;
            }
            base.OnApplyXProperty();

            ctlTextBlock.Foreground = this.XForeground == null ? null : XForeground.Brush;
            ctlTextBlock.Effect = GetDropShadowEffect();
        }

        protected override void OnActive(PartContext context)
        {
            base.OnActive(context);
        }

        /// <summary>
        /// 获取阴影对象
        /// </summary>
        /// <returns></returns>
        protected DropShadowEffect GetDropShadowEffect()
        {
            if (XIsShowShadow != null && XIsShowShadow.Value)
            {
                DropShadowEffect effect = new DropShadowEffect();
                if (!string.IsNullOrWhiteSpace(XShadowColor))
                {
                    effect.Color = (Color)ColorConverter.ConvertFromString(XShadowColor);
                }
                if (XShadowDirection != null)
                {
                    effect.Direction = XShadowDirection.Value;
                }
                if (XShadowDepth != null)
                {
                    effect.ShadowDepth = XShadowDepth.Value;
                }
                return effect;
            }
            else
                return null;

        }
    }
}
